Passed
Push — develop ( 92c330...5e907d )
by Aristeides
03:19
created

kirkiDependencies.showKirkiControl   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
c 2
b 1
f 0
nc 8
dl 0
loc 30
rs 6.7272
nop 1
1
var kirkiDependencies = {
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
2
3
	listenTo: {},
4
5
	init: function() {
6
		var self = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
7
8
		wp.customize.control.each( function( control ) {
9
			self.showKirkiControl( control );
10
		} );
11
12
		_.each( self.listenTo, function( slaves, master ) {
13
			_.each( slaves, function( slave ) {
14
				wp.customize( master, function( setting ) {
15
					var setupControl = function( control ) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
16
						var setActiveState,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
17
							isDisplayed;
18
19
						isDisplayed = function() {
20
							return self.showKirkiControl( wp.customize.control( slave ) );
21
						};
22
						setActiveState = function() {
23
							control.active.set( isDisplayed() );
24
						};
25
26
						setActiveState();
27
						setting.bind( setActiveState );
28
						control.active.validate = isDisplayed;
29
					};
30
					wp.customize.control( slave, setupControl );
31
				} );
32
			} );
33
		} );
34
	},
35
36
	/**
37
	 * Should we show the control?
38
	 *
39
	 * @since 3.0.17
40
	 * @param {string|object} control - The control-id or the control object.
41
	 * @returns {bool}
42
	 */
43
	showKirkiControl: function( control ) {
44
		var self     = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
45
			show     = true,
46
			isOption = (
47
				control.params && // Check if control.params exists.
48
				control.params.kirkiOptionType &&  // Check if option_type exists.
49
				'option' === control.params.kirkiOptionType &&  // We're using options.
50
				control.params.kirkiOptionName && // Check if option_name exists.
51
				! _.isEmpty( control.params.kirkiOptionName ) // Check if option_name is not empty.
52
			),
53
			i;
54
55
56
		if ( _.isString( control ) ) {
57
			control = wp.customize.control( control );
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter control. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
58
		}
59
60
		// Exit early if control not found or if "required" argument is not defined.
61
		if ( 'undefined' === typeof control || ( control.params && _.isEmpty( control.params.required ) ) ) {
62
			return true;
63
		}
64
65
		// Loop control requirements.
66
		for ( i = 0; i < control.params.required.length; i++ ) {
67
			if ( ! self.checkCondition( control.params.required[ i ], control, isOption, 'AND' ) ) {
68
				show = false;
69
			}
70
		}
71
		return show;
72
	},
73
74
	/**
75
	 * Check a condition.
76
	 *
77
	 * @param {Object} requirement - The requirement, inherited from showKirkiControl.
78
	 * @param {Object} control - The control object.
79
	 * @param {bool}   isOption - Whether it's an option or not.
80
	 * @param {string} relation - Can be one of 'AND' or 'OR'.
81
	 */
82
	checkCondition: function( requirement, control, isOption, relation ) {
83
		var self          = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
84
			childRelation = ( 'AND' === relation ) ? 'OR' : 'AND',
85
			requirementShow,
0 ignored issues
show
Unused Code introduced by
The variable requirementShow seems to be never used. Consider removing it.
Loading history...
86
			nestedItems,
87
			i;
88
89
		// Tweak for using active callbacks with serialized options instead of theme_mods.
90
		if ( isOption && requirement.setting ) {
91
92
			// Make sure we don't already have the option_name in there.
93
			if ( -1 === requirement.setting.indexOf( control.params.kirkiOptionName + '[' ) ) {
94
				requirement.setting = control.params.kirkiOptionName + '[' + requirement.setting + ']';
95
			}
96
		}
97
98
		// If an array of other requirements nested, we need to process them separately.
99
		if ( 'undefined' !== typeof requirement[0] && 'undefined' === typeof requirement.setting ) {
100
			nestedItems = [];
101
102
			// Loop sub-requirements.
103
			for ( i = 0; i < requirement.length; i++ ) {
104
				nestedItems.push( self.checkCondition( requirement[ i ], control, isOption, childRelation ) );
105
			}
106
107
			// OR relation. Check that true is part of the array.
108
			if ( 'OR' === childRelation ) {
109
				return ( -1 !== nestedItems.indexOf( true ) );
110
			}
111
112
			// AND relation. Check that false is not part of the array.
113
			return ( -1 === nestedItems.indexOf( false ) );
114
		}
115
116
		// Early exit if setting is not defined.
117
		if ( 'undefined' === typeof wp.customize.control( requirement.setting ) ) {
118
			return true;
119
		}
120
121
		self.listenTo[ requirement.setting ] = self.listenTo[ requirement.setting ] || [];
122
		if ( -1 === self.listenTo[ requirement.setting ].indexOf( control.id ) ) {
123
			self.listenTo[ requirement.setting ].push( control.id );
124
		}
125
126
		return self.evaluate(
127
			requirement.value,
128
			wp.customize.control( requirement.setting ).setting._value,
129
			requirement.operator
130
		);
131
	},
132
133
	/**
134
	 * Figure out if the 2 values have the relation we want.
135
	 *
136
	 * @since 3.0.17
137
	 * @param {mixed} value1 - The 1st value.
138
	 * @param {mixed} value2 - The 2nd value.
139
	 * @param {string} operator - The comparison to use.
140
	 * @returns {bool}
141
	 */
142
	evaluate: function( value1, value2, operator ) {
143
		var found  = false,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
144
			result = null;
145
146
		if ( '===' === operator ) {
147
			return value1 === value2;
148
		}
149
		if ( '==' === operator || '=' === operator || 'equals' === operator || 'equal' === operator ) {
150
			return value1 == value2;
151
		}
152
		if ( '!==' === operator ) {
153
			return value1 !== value2;
154
		}
155
		if ( '!=' === operator || 'not equal' === operator ) {
156
			return value1 != value2;
157
		}
158
		if ( '>=' === operator || 'greater or equal' === operator || 'equal or greater' === operator ) {
159
			return value2 >= value1;
160
		}
161
		if ( '<=' === operator || 'smaller or equal' === operator || 'equal or smaller' === operator ) {
162
			return value2 <= value1;
163
		}
164
		if ( '>' === operator || 'greater' === operator ) {
165
			return value2 > value1;
166
		}
167
		if ( '<' === operator || 'smaller' === operator ) {
168
			return value2 < value1;
169
		}
170
		if ( 'contains' === operator || 'in' === operator ) {
171
			if ( _.isArray( value1 ) && _.isArray( value2 ) ) {
172
				_.each( value2, function( value ) {
173
					if ( value1.includes( value ) ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if value1.includes(value) is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
174
						found = true;
175
						return false;
176
					}
177
                } );
178
				return found;
179
			}
180
			if ( _.isArray( value2 ) ) {
181
				_.each( value2, function( value ) {
182
					if ( value == value1 ) { // jshint ignore:line
183
						found = true;
184
					}
185
				} );
186
				return found;
187
			}
188
			if ( _.isObject( value2 ) ) {
189
				if ( ! _.isUndefined( value2[ value1 ] ) ) {
190
					found = true;
191
				}
192
				_.each( value2, function( subValue ) {
193
					if ( value1 === subValue ) {
194
						found = true;
195
					}
196
				} );
197
				return found;
198
			}
199
			if ( _.isString( value2 ) ) {
200
				return -1 < value1.indexOf( value2 );
201
			}
202
		}
203
		return ( null === result ) ? true : result;
204
	}
205
};
206
207
jQuery( document ).ready( function() {
208
	kirkiDependencies.init();
209
} );
210